package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.list.feign.SearchFeign;
import com.atguigu.gmall.web.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * projectName: gmall
 *
 * @author: 陈鹏
 * time: 2022/10/5 14:01 星期三
 * description: 搜索页面的控制层
 */
@Controller
@RequestMapping(value = "/page/search")
public class SearchController {

    @Autowired
    private SearchFeign searchFeign;

    @Value("${page.url}")
    public String pageUrl;

    /**
     * 打开搜索页面
     * @param searchData
     * @param model
     * @return
     */
    @GetMapping
    public String list(@RequestParam Map<String,String> searchData, Model model){
        //远程调用搜索微服务
        Map<String, Object> result = searchFeign.search(searchData);
        //将搜索结果放入共享域
        model.addAllAttributes(result);
        //将搜索的条件也放入共享域
        model.addAttribute("searchData",searchData);
        //拼接当前的url
        String url = getUrl(searchData);
        model.addAttribute("url",url);
        //获取排序的url
        String softUrl = getSoftUrl(searchData);
        model.addAttribute("softUrl",softUrl);
        //获取真实的页码
        Integer pageNum = getPage(searchData.get("pageNum"));
        //符合条件的总数据量
        long totalHits = Long.parseLong(result.get("totalHits").toString());
        //每页显示的条数
        Integer size = 50;
        //分页对象初始化
        Page pageInfo =
                new Page<>(totalHits, pageNum, size);
        model.addAttribute("pageInfo",pageInfo);
        //存储商品详情页的域名
        model.addAttribute("pageUrl",pageUrl);
        //打开搜索页面
        return "list";
    }

    /**
     * 计算页码
     * @param pageNum
     * @return
     */
    private Integer getPage(String pageNum) {
        try{
            int i = Integer.parseInt(pageNum);
            return i>0?i:1;
        }catch (Exception e){
            return 1;
        }
    }

    /**
     *  拼接本次请求后的url是什么
     * @param searchData
     * @return
     */
    private String getUrl(Map<String,String> searchData){
        //url初始化
        StringBuffer sb = new StringBuffer("/page/search?");
        //拼接参数
        searchData.entrySet().stream().forEach(entry -> {
            //获取参数的key
            String key = entry.getKey();
            if(!key.equals("pageNum")){
                //获取参数的value
                String value = entry.getValue();
                //拼接
                sb.append(key + "=" + value + "&");
            }
        });
        //返回
        return sb.toString().substring(0,sb.toString().length()-1);
    }

    /**
     * 排序的url
     * @param searchData
     * @return
     */
    private String getSoftUrl(Map<String,String> searchData){
        //url初始化
        StringBuffer sb = new StringBuffer("/page/search?");
        //拼接参数
        searchData.entrySet().stream().forEach(entry->{
            //获取参数的key
            String key = entry.getKey();
            if(!key.equals("softRule") &&
                    !key.equals("softField") &&
                    !key.equals("pageNum")){
                //获取参数的value
                String value = entry.getValue();
                //拼接
                sb.append(key + "=" + value + "&");
            }
        });
        //返回
        return sb.toString().substring(0,sb.toString().length()-1);
    }
}
